using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.Presenters.Views.Template;
using CMS.Presenters.Presenters.Template;
using CMS.Core.Domain;
using System.Collections.Generic;
using CMS.Data;
using ProjectBase.Utils;

namespace CMS.Web.Template
{
    public partial class BaiVietDetail : ViewUserControl, IBaiVietDetailView
    {
        private BaiViet objBV;
        private IList<BaiViet> objBVs;
        BaiVietDetailPresenter presenter;

        public BaiVietDetail()
        {
            this.presenter = new BaiVietDetailPresenter(this, NHibernateDaoFactory.Instance.GetBaiVietDao());
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.presenter.GetBaiVietSelected(this.VId);
            this.presenter.GetAllBaiVietOtherBaiViet(this.CatId, this.VId);
            if (!this.IsPostBack)
            {
                this.grdArticles.DataSource = this.objBVs;
                this.grdArticles.DataBind();
            }
        }

        protected void grdArticles_RowCreated(object sender, GridViewRowEventArgs e)
        {
            BaiViet baiviet = (BaiViet)e.Row.DataItem;
            if (baiviet != null)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    foreach (Control c in e.Row.Cells[0].Controls)
                    {
                        //the first article
                        if ((c.ClientID == "tblOthersArticles"))
                        {
                            HtmlTable tbl = (HtmlTable)c;
                            c.Visible = true;

                            LinkButton lblTitle = (LinkButton)tbl.Rows[0].Cells[0].FindControl("lblTitleOther");                            
                            if (lblTitle != null)
                            {
                                lblTitle.Text = baiviet.TieuDe.ToString();
                                lblTitle.Attributes.Add("onclick", "return doView(\"" + this.BaseUrl + "?cid=" + this.CatId + "&vid=" + baiviet.ID.ToString() + "\");");
                            } 
                        }
                    }
                }
            }


            if (e.Row.RowType == DataControlRowType.Pager)
            {
                foreach (Control c in e.Row.Cells[0].Controls)
                {
                    if (c.GetType().ToString().Equals("System.Web.UI.WebControls.PagerTable"))
                    {
                        Table tbl = (Table)c;
                        TableCell cell = new TableCell();
                        cell.Text = "Trang ";
                        cell.CssClass = "Gird_Paging";
                        cell.HorizontalAlign = HorizontalAlign.Right;
                        tbl.Rows[0].Cells.AddAt(0, cell);
                    }
                }
            }

        }

        protected void grdArticles_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.grdArticles.DataSource = this.LstBaiViet;
            grdArticles.PageIndex = e.NewPageIndex;
            grdArticles.DataBind();
        }

        #region IBaiVietDetailView Members

        public CMS.Core.Domain.BaiViet BaiVietSelected
        {
            get
            {
                return this.objBV;
            }
            set
            {
                if (value != null)
                {
                    this.objBV = value;
                    lblTitle.Text = value.TieuDe;
                    lblSummary.Text = value.TomTat;
                    lblContent.Text = value.NoiDung;
                    lblAuthor.Text = value.NguonTin;
                    if ((value.AnhDaiDien != "") && (CFile.Instance.ExistsFile("", value.AnhDaiDien)))
                    {
                        //System.Drawing.Image img = System.Drawing.Image.FromFile(this.FullBaseUrl + "sharedoc/" + value.AnhDaiDien);
                        //int iImgW = img.Width;
                        //int iImgH = img.Height;
                        //img.Dispose();

                        //if (iImgW > 300)
                        //{
                        //    imgImage.Width = (iImgW > 500) ? (500) : (iImgW);
                        //    tblImg.Align = "center";
                        //    tblImg.CellSpacing = 5;
                        //    lblSummary.Visible = false;                           
                        //}

                        imgImage.Visible = true;
                        imgImage.ImageUrl = this.FullBaseUrl + "sharedoc/" + value.AnhDaiDien;
                        //imgImage.Attributes.Add("onclick", "return doImg(\"" + this.FullBaseUrl + "sharedoc/" + value.AnhDaiDien + "\"," + iImgW + "," + iImgH + ");");

                        tblImg.Visible = true;

                        if (!value.ChuThichAnh.Equals(""))
                        {
                            lblImgNote.Text = value.ChuThichAnh;
                            trImgNote.Visible = true;
                        }
                        else
                        {
                            trImgNote.Visible = false;
                        }
                    }
                    else
                    {
                        tblImg.Visible = false;
                    }                    
                }
                else
                {                    
                    lblSummary.Text = GetLocalResourceObject("msgNoInfo").ToString();
                    lblSummary.CssClass = "Main_Text";
                }
                this.objBV = value;
            }
        }

        public System.Collections.Generic.IList<CMS.Core.Domain.BaiViet> LstBaiViet
        {
            get
            {
                return this.objBVs;
            }
            set
            {
                this.objBVs = value;
                if (value.Count <= 0)
                {
                    this.trSpace.Visible = false;
                }
            }
        }

        #endregion
    }
}